﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;

namespace RSSR
{
    public partial class Page1 : PhoneApplicationPage
    {
        WebClient aWebclient = new WebClient();
        RSSFeed CurrentFeed; // = new RSSFeed();
        public Page1()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            aWebclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(aWebclient_DownloadStringCompleted);
            aWebclient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(aWebclient_DownloadProgressChanged);
        }

        void aWebclient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            if (progressBar1.Value == 100) progressBar1.Value = 0; else progressBar1.Value+=5;

        }

        private void btnLoadInfo_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                aWebclient.DownloadStringAsync(new Uri(txtFeedURL.Text));
            }
            catch (System.UriFormatException)
            {
                MessageBox.Show("Invalid Feed URL.");
            }

            
        }

        void aWebclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            progressBar1.Value = 0;

            try
            {
                lbFeedInfo.Items.Clear();

                // Checking for errors.
                if (e.Error != null) throw new Exception();
                
                // Parsing Feed Info
                XElement MyFeed = XElement.Parse(e.Result);
                System.Collections.Generic.Dictionary<string, string> FeedInfo = new Dictionary<string, string>();
                CurrentFeed = new RSSFeed
                {
                    FeedDetails = MyFeed.Element("channel").Element("description").Value,
                    FeedTitle = MyFeed.Element("channel").Element("description").Value,
                    FeedURL = txtFeedURL.Text
                };
                FeedInfo.Add("Title", CurrentFeed.FeedTitle);
                FeedInfo.Add("Description", CurrentFeed.FeedDetails);
                FeedInfo.Add("URL", CurrentFeed.FeedURL);

                lbFeedInfo.ItemsSource = FeedInfo;
                btnSaveFeed.IsEnabled = true;
            }
            catch
            {
                // Display Error Message.
                MessageBox.Show("Error in retrieving the feed data, please check the feed URL and your Internet connectivity.");
            }
            finally
            {

            }

        }

        private void btnSaveFeed_Click(object sender, RoutedEventArgs e)
        {
            ConfigFile.AddRSSFeed(CurrentFeed);
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        
    }
}